package com.chushouya.common.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
 * 队列消息包装类
 * @author chushouya team
 */
@Data
public class QueueMessage<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 消息ID
     */
    private String messageId;

    /**
     * 消息类型
     */
    private String messageType;

    /**
     * 消息内容
     */
    private T data;

    /**
     * 创建时间
     */
    private String createTime;

    /**
     * 延时时间（毫秒）
     */
    private Long delayTime;

    /**
     * 重试次数
     */
    private Integer retryCount = 0;

    /**
     * 最大重试次数
     */
    private Integer maxRetryCount = 3;

    /**
     * 扩展属性
     */
    private Map<String, Object> properties;

    public QueueMessage() {
        this.createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        this.messageId = java.util.UUID.randomUUID().toString();
    }

    public QueueMessage(T data) {
        this();
        this.data = data;
    }

    public QueueMessage(String messageType, T data) {
        this(data);
        this.messageType = messageType;
    }

    /**
     * 增加重试次数
     */
    public void incrementRetryCount() {
        this.retryCount++;
    }

    /**
     * 是否超过最大重试次数
     */
    @JsonIgnore
    public boolean isExceedMaxRetry() {
        return this.retryCount >= this.maxRetryCount;
    }
}
